/**
 * 操作数组的类
 */
class MyArray {
    constructor(capacity) {
        this.arr = new Array(capacity);
        this.size = 0;
    }
    /**
     * 插入元素
     */
    insert(index, element) {
        //判断访问下标是否超出范围
        if (index < 0 || index > this.size) {
            throw Error('超出数组的实际元素范围');
        }
        //如果实际元素达到数组容量上线，数组扩容
        if (this.size >= this.arr.length) {
            this.resize();
        }
        //从右向左循环，逐个元素向右挪一位。
        for (let i = this.size - 1; i >= index; i--) {
            this.arr[i + 1] = this.arr[i];
        }
        //腾出的位置放入新元素
        this.arr[index] = element;
        this.size++;
    }
    /**
     * 数组扩容
     */
    resize() {
        let newArray = new Array(this.arr.length * 2);
        for (let i = 0; i < this.size; i++) {
            newArray[i] = this.arr[i];
        }
        this.arr = newArray;
    }
    /**
     * 删除元素
     */
    delete(index) {
        //判断访问下标是否超出范围
        if (index < 0 || index >= this.size) {
            throw Error('超出数组实际元素范围！');
        }
        let deletedElement = this.arr[index];
        for (let i = index; i < this.size - 1; i++) {
            this.arr[i] = this.arr[index + 1];
        }
        this.size--;
        return deletedElement;
    }
    /**
     * 输出数组
     */
    output() {
        for (let i = 0; i < this.size; i++) {
            console.log(this.arr[i]);
        }
    }
    static main(args) {
        let myArr = new MyArray(4);
        myArr.insert(0, 3);
        myArr.insert(1, 7);
        myArr.insert(2, 9);
        myArr.insert(3, 5);
        myArr.insert(1, 6);
        myArr.insert(5, 8);
        myArr.delete(3);
        myArr.output();
    }
}

MyArray.main()